#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author   : Moty
# @Time     : 2024/1/22 0:12
# @File     : kc16_0A.py
# @Project  : jt808
from utils.common.dicUtil import parse_terminal_param_reply_hex


def parse_0A_Billing_Model_Request_Response(body):
    # 解析0x0104 查询终端参数应答
    data = {
        "桩编号": body[0:14],
        "计费模型编号": body[14:18] + "(" + body[14:18] + ")",
        "尖费电费费率": f"{int(body[18:26], 16)/100000}({body[18:26]})",
        "尖服务费费率": f"{int(body[26:34], 16)/100000}({body[26:34]})",
        "峰电费费率": f"{int(body[34:42], 16)/100000}({body[34:42]})",
        "峰服务费费率": f"{int(body[42:50], 16)/100000}({body[42:50]})",
        "平电费费率": f"{int(body[50:58], 16)/100000}({body[50:58]})",
        "平服务费费率": f"{int(body[58:66], 16)/100000}({body[58:66]})",
        "谷电费费率": f"{int(body[66:74], 16)/100000}({body[66:74]})",
        "谷服务费费率": f"{int(body[74:82], 16)/100000}({body[74:82]})",
        "计损比例": f"{int(body[82:84], 16)}({body[82:84]})",
    }

    # 生成时段费率号
    time_slots = [f"{hour:02d}:{minute:02d}-{hour:02d}:{(minute + 30) % 60:02d} 时段费率号"
                  for hour in range(24) for minute in range(0, 60, 30)]

    for i, time_slot in enumerate(time_slots):
        start = 84 + i * 2
        end = start + 2
        data[time_slot] = time_period_rate_num(body[start:end]) + f"({body[start:end]})"

    # 格式化输出
    formatted_data = "\n".join(f"{key}: {value}" for key, value in data.items())
    return formatted_data


def time_period_rate_num(result):
    rates = {"00": "尖费率", "01": "峰费率", "02": "平费率", "03": "谷费率"}
    return rates.get(result, "未知结果")


__all__ = ['parse_0A_Billing_Model_Request_Response']
